//
//  ServiceCatSubSegViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/30.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "ServiceCatSubSegNaviView.h"

#import "ServiceCatSubListViewController.h"
#import "MessageCenterViewController.h"
#import "SearchViewController.h"

#import "ServiceCatSubSegViewController.h"


@interface ServiceCatSubSegViewController ()
<UITextFieldDelegate,
XLPageViewControllerDelegate,
XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) ServiceCatSubSegNaviView *naviView;

@property (nonatomic, strong) NSMutableArray <ProClassificationModel *>*dataArray;

@end

@implementation ServiceCatSubSegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    
    if (self.scId) {
        [self request_catData];
    }
    
    if (self.model) {
        
        self.dataArray = @[].mutableCopy;
        self.dataArray = self.model.sysClassificationList;
        [self initView];
    }
}

- (void)initView {
    
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    [RACObserve([TT_MessageManager manager], totalUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].totalUnReadCount > 0) {
            self.naviView.msgBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].totalUnReadCount];
            self.naviView.msgBtn.badgeBGColor = [UIColor redColor];
        } else {
            self.naviView.msgBtn.badgeValue = @"";
            self.naviView.msgBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    [self.view addSubview:self.naviView];
    
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kStatusBarHeight);
        make.height.mas_equalTo(NavBar_Height);
    }];
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = UIColorHex(0xff4242);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    config.titleNormalFont = [UIFont systemFontOfSize:14];
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    config.separatorLineHidden = true;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
    config.shadowLineWidth = 20;
    config.titleSpace = 20;    
    
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];
//    self.pageViewController.view.frame = self.view.bounds;
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    
    if (self.selectIndex) {
        self.pageViewController.selectedIndex = self.selectIndex;
    }
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.naviView.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
    
    [self initViewAction];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    SearchViewController *vc = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    return NO;
}

- (void)initViewAction {
    
    WEAKSELF
    self.naviView.ServiceCatSubSegNaviBlock = ^(ServiceCatNaviType type) {
      
        switch (type) {
            case ServiceCatNaviType_back:{
                [weakSelf goBack];
            } break;
            case ServiceCatNaviType_im:{
                [weakSelf goMsg];
            } break;
            default:
                break;
        }
    };
}

- (void)goMsg {
    MessageCenterViewController *vc = [[MessageCenterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    ServiceCatSubListViewController *vc = [[ServiceCatSubListViewController alloc] initWithParentId:self.dataArray[index].scId];
    return vc;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.dataArray[index].parentName;
}

- (NSInteger)pageViewControllerNumberOfPage {
    
    return self.dataArray.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    NSLog(@"切换到了：%@",self.dataArray[index].parentName);
}

- (ServiceCatSubSegNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"ServiceCatSubSegNaviView"owner:nil options:nil].firstObject;
        _naviView.searchTF.delegate = self;

    }
    return _naviView;
}

- (void)request_catData {
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:@(self.scId) forKey:@"scId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ChildCategory];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSArray *oneList = [data valueForKey:@"data"];

                if ([oneList isKindOfClass:[NSArray class]]) {
                    
                    weakSelf.dataArray = @[].mutableCopy;
                    for (int i = 0; i < oneList.count; i++) {
                        ProClassificationModel *oneModel = [ProClassificationModel modelWithDictionary:oneList[i]];

                        if (i==0) {
                            oneModel.isSelect = YES;
                        }
                        
                        [weakSelf.dataArray addObject:oneModel];
                    }
                    [weakSelf initView];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
}

@end
